// pages/category/category.js
let {getCategory,getRight,getCategory459} = require("../../api/home")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageURL:[
      "https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/29ada61b2cc607c16e8cfe360275cff6.png?w=800&h=800"
    ],
    imageURL1:[
      

    ],
      itemIndex:0,
      category1:[

      ]
    
  },
  // 切换效果
  setItem(event){
    console.log(event.currentTarget.dataset);
    let index = event.currentTarget.dataset.index;
    this.setData({
      itemIndex:index
    })
    this.getNavData(index);
    console.log(" this.category=>",this.data.category1);
    
  },
  getNavData(index){
   { //没有数据才请求数据
    if(this.data.category1[index].id!=459){
      getRight({cat_id:this.data.category1[index].id}).then((data)=>{
        console.log("jia=>",data);
        let category_list =data.data[0].category_list
        console.log(category_list);
      let cat=  category_list.filter(key=>{
        return key.view_type== 'category_group' 
         
        })
        console.log("filter",cat);
       

        let ikun= cat.filter(item => item.body.is_expand==false );
        // console.log("cat[ikun]",cat[ikun].body.product_list);
       console.log("ikun",ikun);
       let ikun1= cat.filter(item => item.body.is_expand==true );
       console.log("ikun1",ikun1);


        let ind =category_list.findIndex(item => item.view_type == 'cells_auto_fill' );
  
        this.setData({
         [ "category1["+index+"].imageURL1"]:category_list[ind].body.items[0].img_url,
         [ "category1["+index+"].all"]:cat,
         [ "category1["+index+"].bad"]:ikun1,
         [ "category1["+index+"].list"]:ikun
        })
      })
    }else if(this.data.category1[index].id=459){
      getCategory459().then(data=>{
        let category_list =data.data[0].category_list
        console.log(category_list);
      let cat=  category_list.filter(key=>{
        return key.view_type== 'category_group' 
         
        })
        console.log("filter",cat);
       

        let ikun= cat.filter(item => item.body.is_expand==false );
        // console.log("cat[ikun]",cat[ikun].body.product_list);
       console.log("ikun",ikun);
       let ikun1= cat.filter(item => item.body.is_expand==true );
       console.log("ikun1",ikun1);


        let ind =category_list.findIndex(item => item.view_type == 'cells_auto_fill' );
  
        this.setData({
         [ "category1["+index+"].imageURL1"]:category_list[ind].body.items[0].img_url,
         [ "category1["+index+"].all"]:cat,
         [ "category1["+index+"].bad"]:ikun1,
         [ "category1["+index+"].list"]:ikun
        })
    })

    }
    
      
    }


    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    getCategory().then(data=>{
      console.log("data1=>",data);
      let arr =data.data.slice(0, 6)
      console.log("arr=>",arr);
      let category=[]
    arr.forEach(item=>{
    category.push({
      id:item.category_id,
      name:item.category_name,
      imageURL1:null,
      list:[
      ],
      bad:[

      ],
      all:[]
      })
    // console.log(" this.category=>", category);
     
    })
    this.setData({
      "category1":category
      
    }) 
    console.log(" this.category=>",this.data.category1);

    // console.log(" this.category=>", category1);
 this.getNavData(0);
    
    })
    getCategory459().then(data=>{
      console.log("id",data);
    })

   
   
  },
  

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    console.log("onReady");

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    console.log("onShow");
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    console.log("onHide");

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})